const axios = require('axios')
/**
 * 开市工作
 * @param ctx
 * @returns {Promise<void>}
 */
exports.stockOpenWork = async function (ctx) {
  try {
    const isMarketOpen = await ctx.services.stockMarketOpen.getStockMarketOpen()
    if (isMarketOpen) {
      await ctx.services.fund.updateAllFundBaseInfo()
      await ctx.services.schedule.updateSchedule('updateFundValuation', {
        open: 'open'
      })
    }
    ctx.body = ctx.resuccess({
      isMarketOpen
    })
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 闭市工作
 * @param ctx
 * @returns {Promise<void>}
 */
exports.stockCloseWork = async function (ctx) {
  try {
    await ctx.services.schedule.updateSchedule('updateFundValuation', {
      open: 'close'
    })
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 初始化上一日净值
 * @param ctx
 * @returns {Promise<void>}
 */
exports.reInitPreNetValue = async function (ctx) {
  try {
    await ctx.services.userNetValue.reInitPreNetValue()
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 定时更新用户资产
 * @param ctx
 * @returns {Promise<void>}
 */
exports.updateUserAsset = async function (ctx) {
  try {
    const userLastTradeDateNetValue = await ctx.services.userNetValue.getUserLastTradeDateNetValue({ user_name: 'xiaobxia' })
    if (userLastTradeDateNetValue.asset) {
      await ctx.services.userAsset.updateUserAssetRaw({ name: 'xiaobxia' }, {
        asset: userLastTradeDateNetValue.asset,
        net_value: userLastTradeDateNetValue.net_value,
        income: userLastTradeDateNetValue.income
      })
    }
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

exports.getDictionaryByKey = async function (ctx) {
  const query = ctx.query
  try {
    const data = ctx.validateData({
      key: { required: true }
    }, query)
    const res = await ctx.services.dictionary.getByKey(data.key)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

exports.getDictionaryByKeyList = async function (ctx) {
  const query = ctx.query
  try {
    const data = ctx.validateData({
      keyList: { required: true }
    }, query)
    const res = await ctx.services.dictionary.getByKeyLst(data.keyList)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

exports.updateDictionaryByKey = async function (ctx) {
  const query = ctx.request.body
  try {
    const data = ctx.validateData({
      key: { required: true },
      value: { required: true, type: 'string' }
    }, query)
    await ctx.services.dictionary.updateByKey(data.key, {
      value: data.value
    })
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

exports.proxyUrl = async function (ctx) {
  const query = ctx.request.body
  try {
    const data = ctx.validateData({
      url: { required: true, type: 'string' }
    }, query)
    await axios.get(data.url).then((result) => {
      ctx.body = ctx.resuccess((result && result.data) || result)
    }).catch((err) => {
      if (err.response && err.response.data) {
        ctx.body = ctx.resuccess(err.response.data)
        return
      }
      if (err.response) {
        ctx.body = ctx.resuccess(err.response)
        return
      }
      ctx.body = ctx.resuccess(err)
    })
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

exports.getErrMsgsByPaging = async function (ctx) {
  const query = ctx.query
  try {
    const data = ctx.validateData({
      current: { type: 'int', required: true },
      pageSize: { type: 'int', required: true }
    }, query)
    let paging = ctx.paging(data.current, data.pageSize)
    // 分页获取
    const result = await ctx.services.errMsg.getErrMsgsByPaging({
      ...data
    }, paging)
    paging.total = result.count
    ctx.body = ctx.resuccess({
      list: result.list,
      page: paging
    })
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}
